﻿
using Models.Common;
using Models.Device;
using Models.TaskPlain;
using NetTaste;
using Newtonsoft.Json;


using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;

using static Models.Common.EnumDeviceState;
using GatewayServerService.Data;
using UtilsBase;
using UtilsORM;
using UtilsORM.MySqlData;
using GatewayServerService.CloudCenter;

namespace GatewayServerService.Device
{
    public class ServiceDeviceRS4851019_MC2
    {
        public static int staticpulse = 0;
        //2路脉冲计数器
        public void callBackUpdate(MQReqModel<object> httpReqModel)
        {
            try
            {
                ModelReceverData modelDeviceData = httpReqModel.toServerData;
                CommandLogModel commandLogModel = httpReqModel.toGatewayData;

                if (modelDeviceData.commandResults.Count() < 1)
                {
                    return;
                }




                ModelCommandAgreement modelCommandAgreement = JsonConvert.DeserializeObject<ModelCommandAgreement>(commandLogModel.parameter);
                ModelCommandAgreementParameter modelCommandAgreementParameter = UtilsObject.DynamicToEntity<ModelCommandAgreementParameter>(modelCommandAgreement.agreementParameter);

                string result = modelDeviceData.commandResults[0].value;
                if (commandLogModel.key == "getCount" && !string.IsNullOrWhiteSpace(result))
                {
                    //2路收到信息如：01 04 10 00 01 67 75 00 00 85 DA 00 01 67 4A 00 00 OF FB 70 49

                    //-------------测试临时注释------------------
                    //if (result.Substring(0, 2) != "01")
                    //{
                    //    //返回错误的结果
                    //    return;
                    //}
                    // string dataHex1 = result.Substring(int.Parse(command.returnResultSubstring.Split(",")[0].ToString()), int.Parse(command.returnResultSubstring.Split(",")[1].ToString()));
                    string dataHex1 = "";
                    if (modelCommandAgreementParameter.channel == 1)
                    {
                        //00 00 85 DA
                        dataHex1 = result.Substring(15, 11);
                    }
                    else if (modelCommandAgreementParameter.channel == 2)
                    {
                        //00 00 OF FB
                        dataHex1 = result.Substring(33, 11);
                    }
                    //-------------测试临时注释------------------
                    int pulse = new UtilsDataTypeChange().hex16Convert10(dataHex1); //-------------测试临时注释------------------
                                                                                    // staticpulse = staticpulse + 3; //-------------测试临时注释------------------

                    Console.WriteLine("pulse:" + pulse + "");
                    // Console.WriteLine("flow:" + flow + "L/min");
                    //查当次总流量 和历史总流量
                    double currentFlow = 0;
                    double totalFlow = 0;
                    int currentPulse = 0;
                    int totalPulse = 0;
                    int secondPulse = 0;
                    double secondflowSpeed = 0;
                    EFYihongIotContext ef = new EFYihongIotContext();
                    List<iot_device_value_current> list = ef.iot_device_value_current.Where(c => c.vc_d_guid == commandLogModel.d_guid).ToList();
                    if (list.Count > 0)
                    {

                        if (pulse == 0)
                        {
                            secondPulse = pulse;

                        }
                        else if (pulse > 0)
                        {

                            DateTime dt1 = (DateTime)list.Where(c => c.vc_key == "currentPulse").FirstOrDefault().vc_add_time;
                            DateTime dt2 = DateTime.Now;
                            TimeSpan ts = dt2 - dt1;
                            secondPulse = pulse - int.Parse(list.Where(c => c.vc_key == "currentPulse").FirstOrDefault().vc_value);
                            if (ts.Seconds == 0)
                            {
                                return;
                            }
                            secondflowSpeed = secondPulse / ts.Seconds / 7.5;
                        }
                        currentPulse = pulse;
                        totalPulse = int.Parse(list.Where(c => c.vc_key == "totalPulse").FirstOrDefault().vc_value) + secondPulse;
                        currentFlow = pulse / 450.0;
                        totalFlow = totalPulse / 450.0;

                    }
                    else
                    {
                        secondPulse = 0;
                        totalPulse = pulse;

                        currentFlow = currentPulse / 450.0;
                        totalFlow = totalPulse / 450.0;

                        secondflowSpeed = 0;

                    }
                    Console.WriteLine("currentPulse:" + currentPulse + "");
                    Console.WriteLine("totalPulse:" + totalPulse + "");
                    Console.WriteLine("currentFlow:" + currentFlow + "L");
                    Console.WriteLine("totalFlow:" + secondflowSpeed + "L");

                    //写日志
                    ServiceCloudDeviceValueLog log = new ServiceCloudDeviceValueLog();
                    log.writeSensorlog(commandLogModel.d_guid, "secondPulse", secondPulse.ToString(), "每秒脉冲数");
                    log.writeSensorlog(commandLogModel.d_guid, "secondflowSpeed", UtilsNumber.GetNumberForLength(secondflowSpeed, 2).ToString(), "流速L/min");
                    log.writeSensorlog(commandLogModel.d_guid, "currentPulse", currentPulse.ToString(), "当前持续脉冲数");
                    log.writeSensorlog(commandLogModel.d_guid, "totalPulse", totalPulse.ToString(), "历史脉冲总数");
                    log.writeSensorlog(commandLogModel.d_guid, "currentFlow", UtilsNumber.GetNumberForLength(currentFlow, 2).ToString(), "当前流量L");
                    log.writeSensorlog(commandLogModel.d_guid, "totalFlow", UtilsNumber.GetNumberForLength(totalFlow, 2).ToString(), "历史流量L");


                }


            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

    }

}
